package com.ia2014.tp.portal2.entities;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.log4j.Logger;

import com.ia2014.tp.portal2.vo.ModuloVO;
import com.ia2014.tp.portal2.enums.ModulosEnum;

/**
 * Representa a un objeto necesario para la configuracion.
 * @author 
 *
 */
@Entity
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="tipo", discriminatorType=DiscriminatorType.STRING)
@Table(name="modulos")
public abstract class Modulo extends AbstractEntity<ModuloVO> {

	@Transient
	private static Logger logger = Logger.getLogger(Articulo.class);
	
	@Id
	@Column(name="idModulo")
	private Integer idModulo;
	
	@Column
	private String ipAddress;
	
	@Column
	private String descripcion;
	

	public Modulo() {
		super();
		// TODO Auto-generated constructor stub
	}

	
	public Integer getIdModulo() {
		return idModulo;
	}


	public void setIdModulo(Integer idModulo) {
		this.idModulo = idModulo;
	}


	public String getIpAddress() {
		return ipAddress;
	}


	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}


	public String getDescripcion() {
		return descripcion;
	}


	public void setDescripcion(String descripcion) {
		this.descripcion = descripcion;
	}

	@Override
	public String toString() {
		return "Modulo [idModulo=" + idModulo + ", ipAddress=" + ipAddress
				+ ", descripcion=" + descripcion + "]";
	}
	
	@Transient
	protected ModuloVO getVOInstance() {
		return new ModuloVO();
	}
	
	@Transient
	public ModuloVO getVO() {
		
		try{
			ModuloVO vo = getVOInstance();
		
			vo.setIdModulo(idModulo);
			vo.setDescripcion(descripcion);
			vo.setIpAddress(ipAddress);
			
			return vo;
		}
   		catch (Exception e){
   			logger.error("Error al tratrar de generar un VO de Modulo.\n Modulo: " + this.descripcion);
   			logger.error(e.toString());
   			logger.debug("Stacktrace", e);
   			return null;
   		}
	}

	@Transient
	public void setVO(ModuloVO vo) {
		
		try{
			
			this.setIdModulo(vo.getIdModulo());
			this.setDescripcion(vo.getDescripcion());
			this.setIpAddress(vo.getIpAddress());
			
		}
   		catch (Exception e){
   			logger.error("Error al tratrar de cargar el Molulo a partir de un VO.\n Modulo: " + vo.getDescripcion());
   			logger.error(e.toString());
   			logger.debug("Stacktrace", e);
   		}
	}
	
	@Transient
	public abstract boolean isModuloPortal();
	
	@Transient
	public abstract boolean isModuloLogistica();
	
	@Transient
	public abstract boolean isModuloDespacho();
	
	@Transient
	public abstract boolean isModuloDeposito();

	@Transient
	public abstract ModulosEnum getTipoModulo();
}
